package cn.bocai.pc.web.controller.interceptors;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.bocai.pc.service.VariableService;
import cn.bocai.pc.util.PCConstants;

public class frontInterceptor implements HandlerInterceptor  {  
	@Autowired
	private VariableService variableService;
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {  
    	//  后台session控制  
        Object user=request.getSession().getAttribute("baUserLogin");  
       /* String returnUrl = request.getRequestURI();  */
        String requestUrl = request.getRequestURI();
        if(null==user){
        	boolean date=false;
        	if(requestUrl.contains("login.do")){//登入的时候放行
        		date=true;
        	}else{
        		 response.setContentType("text/html");  
                 response.setCharacterEncoding("utf-8");  
                 PrintWriter out = response.getWriter();    
                 StringBuilder builder = new StringBuilder();    
                 builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");    
                 builder.append("alert(\"请求超时，请重新登陆！\");");    
                 /*builder.append("window.location.href=\""+variableService.selectVariable("HTML_URL")+"/login.jsp\";");//之前的写法 */ 
                 builder.append("window.location.href=\"/login.jsp\";");
                 builder.append("</script>");    
                 out.print(builder.toString());    
                 out.close(); 
        	}
              /*  request.getRequestDispatcher("/login.jsp").forward(request, response); */
                return date;
        }  else {
        	 return true;  
		}
    }  
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {  
    }  
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o, Exception e) throws Exception {  
    }  
}